home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 977 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. From: ajay@lehman.com (Ajay Kamdar)
  2. Message-ID: <4k4noe$igl@jabba.lehman.com>
  3. X-Original-Date: 5 Apr 1996 22:17:34 -0500
  4. Path: in1.uu.net!bounce-back
  5. Date: 06 Apr 96 05:35:59 GMT
  6. Approved: fjh@cs.mu.oz.au
  7. Newsgroups: comp.std.c++
  8. Subject: Re: sample auto_ptr template
  9. Organization: Lehman Brothers, Inc.
  10. References: <009A04DA6A831C40.49800EAC@ittpub.nl> <4k0m72$gm1@jabba.lehman.com> <bill-0504961003150001@bgibbons.vip.best.com>
  11. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  12.     iQBFAgUBMWYCyeEDnX0m9pzZAQE1aAF8D7WOFVCx6rLwgcci+7TFxQL1ptgaXFtt
  13.     cNk6P5/izYomL79x2Y6bs4eAPIVm6b4k
  14.     =4Vwz
  15.  
  16. In article <bill-0504961003150001@bgibbons.vip.best.com>,
  17. Bill Gibbons <bill@gibbons.org> wrote:
  18. >
  19. >Transfer of ownership is not the end goal - the end goal is
  20. >to make auto_ptr useful for the "resource acquisition is
  21. >initialization" idiom.  That is very painful without transfer
  22. >of ownership.
  23. >
  24. >In particular, if you want to do the resource acquisition in a
  25. >function called by the function which needs to hold the resource,
  26. >there is no good exception-safe way to pass the pointer from the
  27. >callee to the caller.
  28. >
  29. >You can get close (at some cost in clarity):
  30. >
  31.        [ snip... ]
  32.  
  33. It is not clear at all that the copy semantics of auto_ptr
  34. are essential for exception-safe transfer of resources.
  35. The same example coded as follows does not use
  36. the copy semantics of auto_ptr:
  37.  
  38.  
  39.     extern X* get_X();   // returns a resource acquired
  40.              // by the callee, to be deleted
  41.              // by the caller.
  42.  
  43.     void f() {
  44.         auto_ptr<X> ptr = get_X();
  45.             // resource allocated by get_X()
  46.         ...
  47.    }
  48.  
  49. And get_X() is not unnecessarily complicated either:
  50.  
  51.    X* get_X()
  52.    {
  53.       auto_ptr<X> p = new X;
  54.  
  55.       // ... stuff that could throw an exception
  56.  
  57.       // We got here. Means normal return.
  58.       return p.release();
  59.    }
  60.  
  61.  
  62. What's wrong with this? It doesn't require copy semantics
  63. for auto_ptr. Yet both the caller and the callee
  64. are exception safe and there is no loss of clarity.
  65.  
  66. -- 
  67. Ajay Kamdar        |    Email: ajay@lehman.com    |    Standard Disclaimer 
  68. Lehman Brothers    |    Phone: (201) 524-5048     |
  69. ---
  70. [ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
  71. [ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
  72. [ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
  73. [ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
  74. [ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]
  75.